#include "AT24C02_driver.h"


void at24c02_byte_write(uint8 address,uint8 data)
{
//	iic_init();
int tmp;
	iic_start0();
	tmp=iic_sendbyte0(WRITE);
	tmp=iic_sendbyte0(address);
	tmp=iic_sendbyte0(data);
	iic_stop0();
	
}

void at24c02_page_write(uint8 address ,uint8 str[],uint8 num)
{
	uint8 temp;
//	iic_init();
	iic_start0();
	iic_sendbyte0(WRITE);
	iic_sendbyte0(address);
	for(temp=0;temp<num;temp++)
	{
		iic_sendbyte0(str[temp]);
	}
	iic_stop0();
}

uint8 at24c02_random_read(uint8 address)
{
	uint8 temp;

	iic_start0();
	iic_sendbyte0(WRITE);
	iic_sendbyte0(address);
	iic_restart0();
	iic_sendbyte0(READ);
	temp=iic_receivebyte0(0);
	iic_stop0();

	return temp;
}

void at24c02_sequential_read(uint8 address,uint8 receive[],uint8 num)
{
	uint8 i,temp;
	iic_start0();
	iic_sendbyte0(WRITE);
	iic_sendbyte0(address);
	iic_restart0();
	iic_sendbyte0(READ);
	
	MCF_I2C0_I2CR&=~MCF_I2C_I2CR_MTX;
	MCF_I2C0_I2CR&=~MCF_I2C_I2CR_TXAK;
	
	temp=MCF_I2C0_I2DR;
	while((MCF_I2C0_I2SR&MCF_I2C_I2SR_IIF)==0) ;
	MCF_I2C0_I2SR&=~MCF_I2C_I2SR_IIF;
	
	for(i=0;i<num-1;i++)
	{
		receive[i]=MCF_I2C0_I2DR;
		while((MCF_I2C0_I2SR&MCF_I2C_I2SR_IIF)==0) ;
		MCF_I2C0_I2SR&=~MCF_I2C_I2SR_IIF;
	}
	
	receive[i]=MCF_I2C0_I2DR;
	MCF_I2C0_I2CR|=MCF_I2C_I2CR_TXAK;
	while((MCF_I2C0_I2SR&MCF_I2C_I2SR_IIF)==0) ;
	MCF_I2C0_I2SR&=~MCF_I2C_I2SR_IIF;
	
	iic_stop0();
}

